package com.foreknow.login;

import java.util.Scanner;

/**
 * @author shkstart
 * @create 2021-07-22 13:37
 */
public class LoginView {
    Scanner sc = new Scanner(System.in);
    int lognum = 3;//记录登录测试的次数
    boolean flag = true;//用于判断登录是否成功
    double balance = 10000;
    String userid;
    String userpas;

    /**
     * 登录判断账号和密码   允许三次
     *
     * @return true 成功   false 失败
     */
    public boolean login() {
        while (flag) {

            System.out.println("请输入账号");
            userid = sc.next();
            System.out.println("请输入密码");
            userpas = sc.next();
            if (userid.equals("admin") && userpas.equals("123456")) {
                flag = false;
            } else {
                lognum--;
                System.out.println("账户名或密码错误，请重新输入今日还剩余" + lognum + "次机会");
                if (lognum <= 0) {
                    System.out.println("吞卡");
                    break;
                }
            }
        }
        return flag;
    }

    public void enterMainMenu() {
        boolean loopFlag = true;
        do {
            System.out.println("                   1 查询余额");
            System.out.println("                   2 存款");
            System.out.println("                   3 取款");
            System.out.println("                   4 修改密码");
            System.out.println("                   5 退       出\n");
            System.out.print("                   请选择(1-5)：");

            char key = CMUtility.readMenuSelection();
            System.out.println();
            switch (key) {
                case '1':
                    selectbalance();
                    loopFlag = goon();
                    break;
                case '2':
                    deposit();
                    loopFlag = goon();
                    break;
                case '3':
                    withdrawal();
                    loopFlag = goon();
                    break;
                case '4':
                    changePassward();
                    loopFlag = goon();
                    break;
                case '5':
                    System.out.print("确认是否退出(Y/N)：");
                    char yn = CMUtility.readConfirmSelection();
                    if (yn == 'Y')
                        loopFlag = false;
                    break;
            }

        } while (loopFlag);
    }

    /**
     * 查询余额
     */
    private void selectbalance() {
        System.out.println("您的余额为" + balance);
    }

    /**
     * 修改密码
     */
    private void changePassward() {
        System.out.println("请输入旧密码");
        String oldpas = sc.next();
        if (!oldpas.equals(userpas)) {
            System.out.println("旧密码输入错误");
            System.out.println("当前密码为" + userpas);
        } else {
            System.out.println("请输入密码");
            String newpas1 = sc.next();
            System.out.println("请再次输入新密码");
            String newpas2 = sc.next();
            if (!newpas1.equals(newpas2)) {
                System.out.println("两次密码不一致，请重新输入");
                System.out.println("当前密码为" + userpas);
            } else {
                userpas = newpas2;
                System.out.println("修改成功");
                System.out.println("当前密码为" + userpas);
            }
        }
    }

    /**
     * 取款
     */
    private void withdrawal() {
        System.out.println("请输入取款金额");
        double withsla = sc.nextDouble();
        if (withsla < 0) {
            System.out.println("用户输入数据非法");
        } else if (withsla > 3000) {
            System.out.println("单次取款金额不能超过3000");
        } else if (withsla % 100 != 0) {
            System.out.println("请取出100的整数倍存款");
        } else if (withsla > balance) {
            System.out.println("余额不足取款失败");

        } else {
            balance -= withsla;
            System.out.println("取款成功");
        }
    }

    /**
     * 存款
     */
    private void deposit() {
        System.out.println("请输入存款金额");
        double depsla = sc.nextDouble();
        if (depsla < 0) {
            System.out.println("输入数据非法");
        } else if (depsla % 100 != 0) {
            System.out.println("请输入100的整数倍存款");
        } else {
            balance += depsla;
            System.out.println("存款成功");
        }

    }

    /**
     * 是否继续操作
     *
     * @return
     */
    public boolean goon() {
        System.out.println("是否继续操作（y/n）");
        char yn = CMUtility.readConfirmSelection();
        if (yn == 'Y') {
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) {
        login login = new login();
        login.view();
        LoginView loginView = new LoginView();
        boolean issuccess = loginView.login();
        if (issuccess == false) {
            loginView.enterMainMenu();
        }

    }


}
